package com.lovcreate.shop.mapp.controller;


import com.lovcreate.shop.api.exception.BaseException;
import com.lovcreate.shop.api.exception.TokenException;
import com.lovcreate.shop.mapp.response.BaseResponse;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolationException;

/**
 * 文件名：
 * 创建人：高博
 * 创建日期 2016-12-20
 * 描  述：控制器抽象类
 * 版本号： 1.0
 **/
@ControllerAdvice
public class AbstractMobileController extends AbstractController {

    private final static Logger logger = org.slf4j.LoggerFactory.getLogger(AbstractMobileController.class);


    @ExceptionHandler(TokenException.class)
    @ResponseBody
    public BaseResponse handleTokenException(TokenException e) {
//        logger.info("[" + e.getId() + "]" + e.getMessage());
        logger.info("[{}] {}", e.getId(), e.getMessage());
        return tokenFailure(e.getMessage());
    }

    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public BaseResponse handleBaseException(BaseException e) {

        logger.info("[" + e.getId() + "]" + e.getMessage(), e);

        return failure(e.getMessage());
    }


    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public BaseResponse ConstraintViolationException(ConstraintViolationException e) {
        logger.error("arguments valid:{}", e.getMessage(), e);

        return failure(e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseResponse Exception(Exception e) {
        logger.error("error:{}", e.getMessage(), e);
        return failure("服务器内部错误");
    }
}
